/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __GUI_TEXTBOX_H__
#define __GUI_TEXTBOX_H__

#pragma once

#include "Label.h"
#include "StringDisplay.h"
#include <string>

namespace gui {

/**
 * \ingroup coreGUI
 *
 * The GUI TextBox class.<br>
 * An area that can hold a string or a whole text block.
 *
 * \date 03-28-2007
 *
 * \author juckel
 * \author dennis
 *
 * \todo Implement multirow textbox w/ scroll capability. 
 * \todo Store input to select previous lines
 * \todo Insert | for input position
 *
 */
class GUI_EXPORT TextBox :
	public Label
{
public:
	TextBox(const std::string &text = "", eTextAlign align = LEFT);
	virtual ~TextBox(void);

	/// Sets the text length, default is 20
	void setTextLength(unsigned int length);

	/// Return the length of the current text
	unsigned int getTextLength() const;

	/// Sets the number of lines
	void setNumberOfLines(int _nl);

	/// Sets the string function
	void setStringFunction(std::string(*fp)(const std::string&));

	/// Process a key event
	virtual void winMsgProc( UINT msg, WPARAM wParam, LPARAM lParam );
	
	/// Set the dimension of the area. Height is ignored. Instead calculated with iNumberOfLines
	void setDimension(int x, int y, int w, int h);

	/// Display a String in this TextBox
	void display(const std::string &_str);

	/// Set if the textbox accepts keyboard input
	void setInput(bool _val);

	void setText(const std::string &text);

	/// Set the maximum number of rows to display
	/// \todo Implement
	void setMaxRows(int) {};

	/// Return if the textbox accepts keyboard input
	const bool getInput() const;

	virtual void displayString(const std::string &_str);

	std::stringstream* getStream() const;

	void triggerChange();

	void setClearOnFirstKey(bool value);
	void setShowCursor(bool value);

	/// Overwrite the font function to include setting the offset of the cursor
	virtual void setFont(IFont* pFont);

	static const bool InTextBox();
	static void InTextBox(bool val);

	/// The draw function of the gui element
	virtual void draw();

	/// Return the type of the gui element
	virtual inline const int getGUIType() const { return GUI_TEXTBOX; }

protected:
	void updateCursorOffset();
	/// Set the focus to the textbox
	virtual void setFocus();
	/// Release the focut from the textbox
	virtual void unsetFocus();
	/// Handle the mouse pressed event
	void onMousePressed(const MouseEvent &me);

	unsigned int uiTextLength;
	unsigned int uiNumberOfLines;
	gui_list     stringOutput;
	std::stringstream stringStream;

	bool			bInput;
	int				iMaxRows;
	unsigned int	uiCursorPosition;

	bool			m_bClearOnFirstKey;
	unsigned int	m_uiDrawCursorOffset;
	bool			m_bShowCursor;
	
	std::string (*stringFunction)(const std::string&);

	static bool s_bInTextBox;
};
} // namespace

#endif
